/*
qnettest - Qt4 application for testing network performance
Copyright (C) 2007  Pyry Haulos

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef TCPPROTO_H
#define TCPPROTO_H

// #define TCPPROTO_DEBUG

#include <QObject>
#include <QTcpSocket>

class QDataStream;
class QMutex;

struct TcpProtoTestData {
	char *data;
	qint32 size;
	QMutex *mutex;
};

class TcpProto:public QObject {
	Q_OBJECT

public:
	TcpProto(QTcpSocket * socket, TcpProtoTestData * data);
	~TcpProto();

	bool send(qint32 send_size, qint32 recv_size);
	bool reply();

	static TcpProtoTestData *generateTestData(qint32 size);
	static void resizeTestData(TcpProtoTestData * data, qint32 size);
	static void freeTestData(TcpProtoTestData * data);

	static qint32 sendHeaderSize() {
		return sizeof(qint32) * 2;
	} static qint32 recvHeaderSize() {
		return sizeof(qint32);
	}

signals:
	void error(QTcpSocket::SocketError socketError);

protected:
	void showLastError();
	void showError(QString error);
	bool skipData(qint32 size);
	bool waitForBytes(qint32 size);

	QTcpSocket *socket;
	TcpProtoTestData *testdata;

	qint32 packet_num;
};

#endif // TCPPROTO_H
